//
//  KthLargest.swift
//  LeetCodeSummary
//
//  Created by 彭西西 on 2020/7/19.
//  Copyright © 2020 WangYonghe. All rights reserved.
//  剑指 Offer 54. 二叉搜索树的第k大节点

import UIKit

/*
 剑指 Offer 54. 二叉搜索树的第k大节点

 给定一棵二叉搜索树，请找出其中第k大的节点
 */

class KthLargest: NSObject {
    
    var arr = [Int]()
    
    func kthLargest(_ root: TreeNode?, _ k: Int) -> Int {

        self.traverseTree(root)
        
        return arr[arr.count-k]
    }
    
    func traverseTree(_ root:TreeNode?){
        if root == nil {
            return
        }
        self.traverseTree(root?.left)
        arr.append(root!.val)
        self.traverseTree(root?.right)
    }
    
}
